package javaweb_work01.classesContrl.Servlet;

import javaweb_work01.classesContrl.Entity.Classes;
import javaweb_work01.classesContrl.Service.*;
import javaweb_work01.main.Utils.PageContrl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "AddClassServlet", urlPatterns = "/addClasses")
public class AddClassesServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决POST接收乱码
        request.setCharacterEncoding("utf-8");
        //接收表单提交信息
        String className = request.getParameter("className");
        int grade = Integer.parseInt(request.getParameter("grade"));
        String teacher = request.getParameter("teacher");
        String watchword = request.getParameter("watchword");

        //检查是否有同名班级
        //先调用CheckClassesService的方法，到数据库中查找输入的班级名的班级信息
        CheckClassesService checkClassesService = new CheckClassesService();
        //得到返回值
        List<Classes> checkClasses = checkClassesService.checkClassesService(className);
        //判断，如返回的结果集中有信息，则证明有同名班级
        if(checkClasses.size() != 0){
            //服务器内跳转回添加页面，并提示有同名班级
            request.setAttribute("classNameError","同名班级已存在");
            request.getRequestDispatcher("addClasses.jsp").forward(request,response);
        }
        //否则回到班级管理主页
        else {
            //调用Service创建班级的方法，传入表单提交的信息
            AddClassesService addClassesService = new AddClassesService();
            addClassesService.addClassesService(new Classes(className, grade, teacher, watchword));

            //回到班级管理界面首页
            //得到班级总数
            CountClassesService countClassesService = new CountClassesService();
            int count = countClassesService.countClassesService();
            //用得到的班级总数和start的属性值构造新的PageContrl对象
            PageContrl pageContrl = new PageContrl(0, 5, count);
            //将新构造的PageContrl对象作为参数传入Service,调用Dao层方法,得到该页的班级数据集合
            ShowClassesService showClassesService = new ShowClassesService();
            List<Classes> classes = showClassesService.showClassesService(pageContrl);

            //得到每个班的人数
            StudentsSumService studentsSumService = new StudentsSumService();
            //声明一个Classes集合用于记录添加班级人数后的Classes对象
            for (Classes cs : classes) {
                int i = studentsSumService.studentsSumService(cs.getCid());
                cs.setSum(i);
            }

            //将得到的PageContrl对象和Classes对象放入session中实现共享
            request.getSession().setAttribute("classesPageContrl", pageContrl);
            request.getSession().setAttribute("showClasses", classes);
            //跳转到班级管理页面
            response.sendRedirect("classes.jsp");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
